from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from PIL import Image
from urllib import request
import os
import base64
import cv2
import numpy as np

image1_name = 'img.png'  # 大图
image2_name = 'img_quekou.png'  # 缺口
#下载图片
def downloadImg(img, imgquekou):

    imagedata1 = base64.b64decode(img)
    imagedata2 = base64.b64decode(imgquekou)
    file1 = open(image1_name, "wb")
    file2 = open(image2_name, "wb")
    file1.write(imagedata1)
    file1.close()
    file2.write(imagedata2)
    file2.close()

    # # 下载滑块图片并存储到本地
    # request.urlretrieve(imgquekou, image1_name)
    # # 下载背景大图并存储到本地
    # request.urlretrieve(img, image2_name)
    pass

#获取浏览器对象
browser =  webdriver.Chrome()
browser.get("http://jenkins.baidu.com/login")
# username  = browser.find_element(By.XPATH,'//*[@id="app"]/div/div/div[2]/div[2]/div/form/div[1]/div/div/input')
username  = browser.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div[2]/div[2]/div[1]/form/div[1]/div/div/input')
username.send_keys('superadmin')

password  = browser.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div[2]/div[2]/div[1]/form/div[2]/div/div/input')
password.send_keys('Ty@123456')


valicode  = browser.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div[2]/div[2]/div[1]/form/div[3]/div/div/div/div')

valicode.click()


#打开保存至本地的缺口页面截图
# quekouimg=Image.open('d://quekou.png')

time.sleep(2)
img  = browser.find_element(By.XPATH,'//*[@id="scream"]').screenshot_as_base64
imgquekou  = browser.find_element(By.XPATH,'//*[@id="puzzle"]/div[1]/div[1]/div[2]/img').screenshot_as_base64
downloadImg(img,imgquekou)

# 获取图片，并灰化
block = cv2.imread(image2_name, 0)
template = cv2.imread(image1_name, 0)

# 二值化之后的图片名称
block_name = 'block.jpg'
template_name = 'template.jpg'
# 将二值化后的图片进行保存
cv2.imwrite(template_name, template)
cv2.imwrite(block_name, block)
block = cv2.imread(block_name)
block = cv2.cvtColor(block, cv2.COLOR_BGR2GRAY)
block = abs(255 - block)
cv2.imwrite(block_name, block)

block = cv2.imread(block_name)
template = cv2.imread(template_name)

# 获取偏移量
result = cv2.matchTemplate(block, template, cv2.TM_CCOEFF_NORMED)  # 查找block图片在template中的匹配位置，result是一个矩阵，返回每个点的匹配结果
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# print("min_val", min_val, "max_val", max_val, "min_loc", min_loc, "max_loc", max_loc)
x, y = np.unravel_index(result.argmax(), result.shape)
# print("x,y:", x, y, 'result.shape:', result.shape)

# # # 显示图片
# w, h = block.shape[::-1]
# print('\nw:', w, ' h:', h)
# show(template, x, y, w, h, block)
# show(template, min_val, max_val, min_loc, max_loc, block)
print(x)
print(y)

y= y+40
print(y)
print(result)



# 获取拖拽的圆球
huakuai = browser.find_element(By.XPATH,'//*[@id="puzzle"]/div[2]/div[2]/img')
# 鼠标点击圆球不松开
action = ActionChains(browser)
action.click_and_hold(huakuai).perform()
# 将圆球滑至相对起点位置的最右边
action.move_to_element_with_offset(to_element=huakuai,xoffset=y, yoffset=0).perform()
# 放开圆球
action.release(on_element=huakuai).perform()

time.sleep(10)
browser.close()









